package org.shilong.UI.businessCard;

import org.shilong.MContext.Businesscard.BusinessCardContext;
import org.shilong.UI.R;
import org.shilong.UI.login.LoginFrame;
import org.shilong.UI.main.MainFrame;
import org.shilong.Utility.IntentUtility;
import org.shilong.Utility.ProgressDialogUtility;
import org.shilong.Utility.SharedPreferenceUtility;
import org.shilong.Utility.ToastUtility;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CreateBusinessCardFrame extends Activity implements OnClickListener{

	private String[] arr;
	private TextView bannerTextivew;

	private EditText businesscard_input_telephonenum, businesscard_input_com_name, businesscard_input_buss, businesscard_input_domain_content, businesscard_input_sex, businesscard_input_age;

	private Button business_button_create;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView( R.layout.layout_create_business_card_content );

		arr = SharedPreferenceUtility.getPersonInfoFromSharedPreference( CreateBusinessCardFrame.this );

		initViews();

		registerEvents();
	}

	/**
	 * 初始化控件
	 */
	private void initViews(){

		//	初始化标题
		bannerTextivew = ( TextView )this.findViewById( R.id.business_card_banner_textivew );
		bannerTextivew.setText( "新建名片" );

		businesscard_input_telephonenum = ( EditText )this.findViewById( R.id.businesscard_input_telephonenum );
		businesscard_input_com_name = ( EditText )this.findViewById( R.id.businesscard_input_com_name );
		businesscard_input_buss = ( EditText )this.findViewById( R.id.businesscard_input_buss );
		businesscard_input_domain_content = ( EditText )this.findViewById( R.id.businesscard_input_domain_content );
		businesscard_input_sex = ( EditText )this.findViewById( R.id.businesscard_input_sex );
		businesscard_input_age = ( EditText )this.findViewById( R.id.businesscard_input_age );

		business_button_create = ( Button )this.findViewById( R.id.business_button_create );
	}

	/**
	 * 初始化事件
	 */
	private void registerEvents(){

		business_button_create.setOnClickListener( this );

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch ( v.getId() ) {
		case R.id.business_button_create:

			createBusinessCardLogic();

			break;

		default:
			break;
		}

	}

	private static String telephone  = null;
	private static String company_name  = null;
	private static String buesiness  = null;
	private static String domain  = null;
	private static String age  = null;
	private static String sex  = null;



	private void createBusinessCardLogic( ){

		telephone  = null;
		company_name  = null;
		buesiness  = null;
		domain  = null;
		age  = null;
		sex  = null;

		System.out.println( "创建名片...." );

		telephone = businesscard_input_telephonenum.getText().toString();
		company_name = businesscard_input_com_name.getText().toString();
		buesiness = businesscard_input_buss.getText().toString();
		domain = businesscard_input_domain_content.getText().toString();
		age = businesscard_input_age.getText().toString();
		sex = businesscard_input_sex.getText().toString();


		if( TextUtils.isEmpty( telephone )){

			ToastUtility.showToast(getApplicationContext(), "输入电话号码" );
			return ;
		}


		if ( TextUtils.isEmpty( company_name )) {
			ToastUtility.showToast(getApplicationContext(), "输入公司名" );
			return ;
		}

		if ( TextUtils.isEmpty( buesiness )) {
			ToastUtility.showToast(getApplicationContext(), "输入行业" );
			return ;
		}

		if ( TextUtils.isEmpty( domain )) {
			ToastUtility.showToast(getApplicationContext(), "输入领域" );
			return ;
		}

		if ( TextUtils.isEmpty( age )) {
			ToastUtility.showToast(getApplicationContext(), "输入年龄" );
			return ;
		}

		if ( TextUtils.isEmpty( sex )) {
			ToastUtility.showToast(getApplicationContext(), "输入性别" );
			return ;
		}

		CreateTask task = new CreateTask(CreateBusinessCardFrame.this);

		task.execute( arr[2], telephone, company_name, buesiness, domain, sex, age, "create" );
	}


	private class CreateTask extends AsyncTask<String, String, String>{

		private Context con;

		public CreateTask( Context mContext ){
			this.con = mContext;
		}

		@Override
		protected String doInBackground(String... params) {

			String rlt = "ohshit";

			rlt = (String)BusinessCardContext.createInstance( BusinessCardContext.TYPE_CREATE_BUSINESS_CARD ).getRlt(params);

			return rlt;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub

			ProgressDialogUtility.cancelDialog();

			System.out.println( "result:" + result );

			if( result.equals("add_business_card_successed")){

				ToastUtility.showToast(con, "创建成功");
				
				IntentUtility.goNextFrame( CreateBusinessCardFrame.this, LoginFrame.class, null );
				
				CreateBusinessCardFrame.this.finish();

			}else if( result.equals( "business_card_had_already" )){}
			ToastUtility.showToast(con, "创建失败");
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub

			ProgressDialogUtility.showDialog(con, "创建中..");
		}

	}
}
